package com.service.admin.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.admin.order.controller.rep.OrderHrComboDetailsRep;
import com.service.admin.order.service.OrderProductHrComboDetailsService;
import com.service.core.exception.CommonException;
import com.service.dao.mapper.OrderProductHrComboDetailsMapper;
import com.service.dao.mapper.ProductHrComboMapper;
import com.service.dao.model.OrderProductHrCombo;
import com.service.dao.model.OrderProductHrComboDetails;
import com.service.dao.model.ProductHrCombo;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("hashRateOrderDetailsService")
public class OrderProductHrComboDetailsServiceImpl extends ServiceImpl<OrderProductHrComboDetailsMapper, OrderProductHrComboDetails> implements OrderProductHrComboDetailsService {

    @Autowired
    private ProductHrComboMapper productHrComboMapper;

    @Override
    public List<OrderHrComboDetailsRep> getOrderHrComboDetailsByOrderId(Integer orderId) {
        List<OrderProductHrComboDetails> orderProductDetails = baseMapper.selectList(new QueryWrapper<OrderProductHrComboDetails>()
                .lambda().eq(OrderProductHrComboDetails::getHrOrderId, orderId));
        if (ObjectUtils.isEmpty(orderProductDetails)) {
            throw new CommonException("此订单不存在订单详情");
        }
        return orderProductDetails.stream().map(item -> ModelMapperUtil.getStrictModelMapper().map(item, OrderHrComboDetailsRep.class)).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOrderDetail(ProductHrCombo hashRateCombo, Integer num, OrderProductHrCombo order) {
        OrderProductHrComboDetails orderProductHrComboDetails =
                ModelMapperUtil.getStrictModelMapper().map(hashRateCombo, OrderProductHrComboDetails.class);
        orderProductHrComboDetails.setHrOrderId(order.getId());
        orderProductHrComboDetails.setOrderNum(num);
        orderProductHrComboDetails.setCreateTime(new Date());
        orderProductHrComboDetails.setComboId(hashRateCombo.getId());
        orderProductHrComboDetails.setCustodyFee(hashRateCombo.getCustodyFee());
        orderProductHrComboDetails.setStartTime(DateUtils.addDays(new Date(),hashRateCombo.getPackageDays()+1));
        orderProductHrComboDetails.setEndTime(order.getEndTime());
        orderProductHrComboDetails.setNeedRenew(hashRateCombo.getNeedRenew());
        orderProductHrComboDetails.setRenewalDays(hashRateCombo.getRenewalDays());
        orderProductHrComboDetails.setNeedPledge(hashRateCombo.getNeedPledge());
        orderProductHrComboDetails.setPledgeNum(hashRateCombo.getPledgeNum());
        orderProductHrComboDetails.setNeedGas(hashRateCombo.getNeedGas());
        orderProductHrComboDetails.setGasNum(hashRateCombo.getGasNum());
        orderProductHrComboDetails.setNeedUsdt(hashRateCombo.getNeedUsdt());
        orderProductHrComboDetails.setUsdtNum(hashRateCombo.getUsdtNum());
        baseMapper.insert(orderProductHrComboDetails);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderProductHrComboDetails updateHrComboInterval(Integer orderId) {
        OrderProductHrComboDetails hrComboDetails = getOne(new QueryWrapper<OrderProductHrComboDetails>().lambda()
                .eq(OrderProductHrComboDetails::getHrOrderId, orderId).last(" limit 1"));

        ProductHrCombo productHrCombo = productHrComboMapper.selectOne(new QueryWrapper<ProductHrCombo>().lambda()
                .eq(ProductHrCombo::getId, hrComboDetails.getComboId()).last(" limit 1"));
        Date tomorrow = DateUtils.addDays(new Date(), productHrCombo.getPackageDays()+1);
        OrderProductHrComboDetails update = new OrderProductHrComboDetails();
        if(!ObjectUtils.isEmpty(productHrCombo)){
            update.setStartTime(com.service.admin.utils.DateUtils.changeDateTo3point(tomorrow));
            update.setEndTime(com.service.admin.utils.DateUtils.changeDateTo3point(DateUtils.addDays(tomorrow, hrComboDetails.getDeadline())));
        }else{
            update.setStartTime(com.service.admin.utils.DateUtils.changeDateTo3point(tomorrow));
            update.setEndTime(com.service.admin.utils.DateUtils.changeDateTo3point(DateUtils.addDays(tomorrow, hrComboDetails.getDeadline())));
        }
        update.setId(hrComboDetails.getId());
        updateById(update);
        return update;
    }
}
